package com.hg.imajax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.hg.imajax.bo.MessageBo;
import com.hg.imajax.service.MessageService;

public class MessageServlet extends HttpServlet{

	private MessageService messageService = new MessageService();
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String flag = req.getParameter("flag");
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=utf-8");
		//用户发起聊天消息咨询
		if("insert".equals(flag)){
			insertUserMsg(req,resp);
			PrintWriter pw = resp.getWriter();
			pw.write("success");
			pw.close();
			return;
		}
		//用户获取客服的回复
		if("userReqMsg".equals(flag)){
			List<MessageBo> msgList = userReqMsg(req,resp);
			Gson gson = new Gson();
			String jsonData = gson.toJson(msgList);//通过gson工具把list集合转成带有json数据格式的字符串
			PrintWriter pw = resp.getWriter();
			pw.write(jsonData);
			pw.close();
			return;
		}
		//客服绑定对话用户
		if("bindUser".equals(flag)){
			Boolean f = bindUser(req,resp);
			PrintWriter pw = resp.getWriter();
			if(f){
				pw.write("true");
			}else{
				pw.write("false");
			}
			pw.close();
			return;
		}
		//插入客户回答
		if("insertService".equals(flag)){
			insertServiceMsg(req,resp);
			PrintWriter pw = resp.getWriter();
			pw.write("success");
			pw.close();
			return;
		}
		//查找客户咨询的问题
		if("serviceReqMsg".equals(flag)){
			List<MessageBo> msgList = serviceReqMsg(req,resp);
			Gson gson = new Gson();
			String jsonData = gson.toJson(msgList);//通过gson工具把list集合转成带有json数据格式的字符串
			PrintWriter pw = resp.getWriter();
			pw.write(jsonData);
			pw.close();
			return;
		}
		
	}
	
	//插入用户发起的聊天咨询消息
	private void insertUserMsg(HttpServletRequest req, HttpServletResponse resp){
		String user_name = req.getParameter("user_name");
		String opper_name = req.getParameter("opper_name");
		String user_msg = req.getParameter("user_msg");
		
		MessageBo bo = new MessageBo();
		bo.setUser_name(user_name);
		bo.setOpper_name(opper_name);
		bo.setMessage_info(user_msg);
		bo.setMessage_type("0");//咨询
		bo.setIf_read("0");//未读
		messageService.insertMsg(bo);
	}
	//用户获取客服的回复
	private List<MessageBo> userReqMsg(HttpServletRequest req, HttpServletResponse resp){
		String user_name = req.getParameter("user_name");
		MessageBo bo = new MessageBo();
		bo.setUser_name(user_name);
		bo.setIf_read("0");//用户未读
		bo.setMessage_type("1");//客服回复
		return messageService.reqMsgList(bo);
	}
	
	//客服绑定服务用户的操作
	private Boolean bindUser(HttpServletRequest req, HttpServletResponse resp){
		String user_name = req.getParameter("user_name");//必须要有用户名
		String opper_name = req.getParameter("opper_name");
		Boolean flag = messageService.bindUserMsg(user_name, opper_name);
		return flag;
	}
	
	//获取绑定服务用户的咨询记录
	private List<MessageBo> serviceReqMsg(HttpServletRequest req, HttpServletResponse resp){
		String user_name = req.getParameter("user_name");
		String opper_name = req.getParameter("opper_name");
		MessageBo bo = new MessageBo();
		bo.setUser_name(user_name);
		bo.setOpper_name(opper_name);
		bo.setIf_read("0");//未读
		bo.setMessage_type("0");//用户咨询
		return messageService.reqMsgList(bo);
	}
	
	//客服插入给用户的回复消息记录
	private void insertServiceMsg(HttpServletRequest req, HttpServletResponse resp){
		String user_name = req.getParameter("user_name");//必须要有用户名
		String opper_name = req.getParameter("opper_name");
		String user_msg = req.getParameter("user_msg");
		
		MessageBo bo = new MessageBo();
		bo.setUser_name(user_name);
		bo.setOpper_name(opper_name);
		bo.setMessage_info(user_msg);
		bo.setMessage_type("1");//回答
		bo.setIf_read("0");//未读
		messageService.insertMsg(bo);
	}
	
}
